package main

import (
	"fmt"
	"github.com/hajimehoshi/ebiten/v2"
	"github.com/hajimehoshi/ebiten/v2/text"
	"github.com/hajimehoshi/ebiten/v2/vector"
	"golang.org/x/image/font/basicfont"
	"image/color"
	"time"
)

// Menu 结构体
type Menu struct {
	X, Y               float32
	Width, Height      float32
	Icons              []*ebiten.Image
	SelectedType       TowerType
	ChangeLineTimer    *time.Timer
	ChangeLineDuration time.Duration
}

const (
	TowerTypeBasic  TowerType = iota // 基础防御塔
	TowerTypeSniper                  // 狙击防御塔
	TowerTypeAoE                     // 范围攻击防御塔
)

// NewMenu 创建一个新的菜单
func NewMenu(x, y, width, height float32) *Menu {
	return &Menu{
		X:                  x,
		Y:                  y,
		Width:              width,
		Height:             height,
		Icons:              make([]*ebiten.Image, 0),
		ChangeLineDuration: 30 * time.Second, // 设置更换线路的倒计时为30秒
	}
}

// Draw 绘制菜单
func (m *Menu) Draw(screen *ebiten.Image, selectedTowerType TowerType) {
	// 绘制菜单背景（渐变效果）
	drawGradientBackground(screen, m.X, m.Y, m.Width, m.Height)

	// 绘制防御塔图标和名称
	for i := 0; i < 3; i++ {
		x := m.X + 20
		y := m.Y + float32(i)*towerIconSize + 20
		towerColor := color.RGBA{0, 0, 255, 255}
		if TowerType(i) == selectedTowerType {
			towerColor = color.RGBA{0, 255, 0, 255} // 选中的图标为绿色
		}
		vector.DrawFilledCircle(screen, x+towerIconSize/2, y+towerIconSize/2, towerIconSize/2, towerColor, false)

		// 绘制防御塔名称
		towerName := getTowerName(TowerType(i))
		fontFace := basicfont.Face7x13
		text.Draw(screen, towerName, fontFace, int(x+towerIconSize+10), int(y+towerIconSize/2+5), color.White)
	}

	// 绘制更换线路倒计时
	if m.ChangeLineTimer != nil {
		remainingTime := time.Until(<-m.ChangeLineTimer.C) // 从 Timer 中获取时间点并计算剩余时间
		if remainingTime > 0 {
			timerText := fmt.Sprintf("Change Line in: %.0f", remainingTime.Seconds()) // 使用 Seconds() 方法
			text.Draw(screen, timerText, basicfont.Face7x13, int(m.X+20), int(m.Y+m.Height-20), color.White)
		}
	}
}

// drawGradientBackground 绘制渐变背景
func drawGradientBackground(screen *ebiten.Image, x, y, width, height float32) {
	// 渐变颜色
	startColor := color.RGBA{50, 50, 50, 255}
	endColor := color.RGBA{100, 100, 100, 255}

	// 绘制渐变背景
	for i := 0; i < int(height); i++ {
		ratio := float32(i) / height
		r := uint8(float32(startColor.R)*(1-ratio) + float32(endColor.R)*ratio)
		g := uint8(float32(startColor.G)*(1-ratio) + float32(endColor.G)*ratio)
		b := uint8(float32(startColor.B)*(1-ratio) + float32(endColor.B)*ratio)
		a := uint8(float32(startColor.A)*(1-ratio) + float32(endColor.A)*ratio)
		vector.StrokeLine(screen, x, y+float32(i), x+width, y+float32(i), 1, color.RGBA{r, g, b, a}, false)
	}
}

// getTowerName 获取防御塔名称
func getTowerName(towerType TowerType) string {
	switch towerType {
	case TowerTypeBasic:
		return "Basic Tower"
	case TowerTypeSniper:
		return "Sniper Tower"
	case TowerTypeAoE:
		return "AoE Tower"
	default:
		return "Unknown Tower"
	}
}

// StartChangeLineTimer 启动更换线路倒计时
func (m *Menu) StartChangeLineTimer() {
	m.ChangeLineTimer = time.NewTimer(m.ChangeLineDuration)
}

// Update 更新菜单状态
func (m *Menu) Update() {
	if m.ChangeLineTimer != nil {
		select {
		case <-m.ChangeLineTimer.C:
			// 倒计时结束，更换线路
			m.ChangeLineTimer = nil
			// 这里可以添加更换线路的逻辑
		default:
			// 倒计时未结束，继续等待
		}
	}
}
